using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Toenda.Foundation.SystemLayer {
	/// <summary>
	/// Class WindowsEnvironment
	/// </summary>
	public class WindowsEnvironment {
		/// <summary>
		/// Self
		/// </summary>
		public static WindowsEnvironment Self = new WindowsEnvironment();

		// ---------------------------------------------------
		// CONSTRUCTORS
		// ---------------------------------------------------

		/// <summary>
		/// Default Ctor
		/// </summary>
		public WindowsEnvironment() {
		}

		// ---------------------------------------------------
		// INTERFACE IMPLEMENTATIONS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PROPERTIES
		// ---------------------------------------------------

		// ---------------------------------------------------
		// EVENTS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PRIVATE MEMBERS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PROTECTED MEMBERS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PUBLIC MEMBERS
		// ---------------------------------------------------

		/// <summary>
		/// Get the value of a variable (The variable target is Machine for default)
		/// </summary>
		/// <param name="key">The variable key.</param>
		/// <returns>Returns the value.</returns>
		public string GetVariable(string key) {
			return this.GetVariable(key, EnvironmentVariableTarget.Machine);
		}

		/// <summary>
		/// Get the value of a variable
		/// </summary>
		/// <param name="key">The variable key.</param>
		/// <param name="target">The variable target.</param>
		/// <returns>Returns the value.</returns>
		public string GetVariable(string key, EnvironmentVariableTarget target) {
			string val = "";

			IDictionary dic = Environment.GetEnvironmentVariables(target);

			foreach(object o in dic) {
				DictionaryEntry de = (DictionaryEntry)o;

				if(de.Key.ToString().ToLower() == key.ToLower()) {
					val += de.Value.ToString();
				}
			}

			return val;
		}

		/// <summary>
		/// Add a variable to a key (The variable target is Machine for default)
		/// </summary>
		/// <param name="key">The variable key.</param>
		/// <param name="value">The variable value.</param>
		/// <returns>Returns true if the key has found and returns false if the key does not exist.</returns>
		public bool AddVariable(string key, string value) {
			return this.AddVariable(key, value, false, EnvironmentVariableTarget.Machine);
		}

		/// <summary>
		/// Add a variable to a key (The variable target is Machine for default)
		/// </summary>
		/// <param name="key">The variable key.</param>
		/// <param name="value">The variable value.</param>
		/// <param name="writeCompleteValue">A value that indicates if we should overwrite the complete value.</param>
		/// <returns>Returns true if the key has found and returns false if the key does not exist.</returns>
		public bool AddVariable(string key, string value, bool writeCompleteValue) {
			return this.AddVariable(key, value, writeCompleteValue, EnvironmentVariableTarget.Machine);
		}

		/// <summary>
		/// Add a variable to a key (The variable target is Machine for default)
		/// </summary>
		/// <param name="key">The variable key.</param>
		/// <param name="value">The variable value.</param>
		/// <param name="target">The variable target.</param>
		/// <returns>Returns true if the key has found and returns false if the key does not exist.</returns>
		public bool AddVariable(string key, string value, EnvironmentVariableTarget target) {
			return this.AddVariable(key, value, false, EnvironmentVariableTarget.Machine);
		}

		/// <summary>
		/// Add a variable to a key
		/// </summary>
		/// <param name="key">The variable key.</param>
		/// <param name="value">The variable value.</param>
		/// <param name="writeCompleteValue">A value that indicates if we should overwrite the complete value.</param>
		/// <param name="target">The variable target.</param>
		/// <returns>Returns true if the key has found and returns false if the key does not exist.</returns>
		public bool AddVariable(string key, string value, bool writeCompleteValue, EnvironmentVariableTarget target) {
			string val = "";
			bool found = false;

			IDictionary dic = Environment.GetEnvironmentVariables(target);

			foreach(object o in dic) {
				DictionaryEntry de = (DictionaryEntry)o;

				if(de.Key.ToString().ToLower() == key.ToLower()) {
					val = de.Value.ToString();
					found = true;
				}
			}

			if(found) {
				if(writeCompleteValue) {
					val = value;
				}
				else {
					val += value;
				}

				Environment.SetEnvironmentVariable(
					key,
					val,
					target
				);

				return true;
			}
			else {
				return false;
			}
		}
	}
}
